package com.cn.ccp.action.course;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.CourseForumBean;
import com.cn.ccp.util.CCPContants;

/**
 * 课程论坛
 * @author Administrator
 *
 */
public class CourseForumManagerAction extends BaseAction{
	
	private Log log = LogFactory.getLog(CourseForumManagerAction.class);
	
	private List<CourseForumBean> list ;
	private List<CourseForumBean> answerlist ;
	private CourseForumBean bean ;
	
	private String title ,lookTitle;
	private String contents ;
	private String courseId ,selectedCourseId;
	private String id ;
	private String type ;//来自哪个页面   details
	private SelectItem[] courseNameSelectItem ;
	
	public CourseForumManagerAction(){
		getpagedetail();
	}
	
	
	
	
	public String showBean(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		try{
			courseId = (String)context.getExternalContext().getRequestParameterMap().get("courseId");
		}catch(Exception e){}
		type = (String)context.getExternalContext().getRequestParameterMap().get("details");
		bean = getCourseForumService().showBean(id);
		
		return "update";
	}
	
	public String addBBS(){
		FacesContext context = FacesContext.getCurrentInstance();
		String type = (String)context.getExternalContext().getRequestParameterMap().get("type");
		courseId = (String)context.getExternalContext().getRequestParameterMap().get("courseId");
		selectedCourseId = (String)context.getExternalContext().getRequestParameterMap().get("selectedCourseId");
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("bbs-type", type);
		return "addbbs";
	}
	
	
	
	public String edit(){
		String contents = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1").toString();
		bean.setContents(contents);
		if(id == null  || id.equals(""))
			add();
		else
			update();

		if(type == null || type .equals("")){
			return "edit";
		}else
			return "details";
	}
	
	
	public String add(){
		
		FacesContext context = FacesContext.getCurrentInstance();
		contents = context.getExternalContext().getRequestParameterMap().get("content1").toString();
		if(bean.getTitle() == null || bean.getTitle().equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_1")));
			return "";
		}
		if(contents == null || contents.equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_2")));
			return "";
		}
		getCourseForumService().install(courseId ,bean.getTitle(), contents, CCPContants.getCurrUserId() ,1);
		
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String formPage = (String) session.getAttribute("bbs-type");
		if(formPage != null  && !formPage.equals("")){
			return formPage;
		}
		return "edit";
	}
	
	public String cancel(){
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String formPage = (String) session.getAttribute("bbs-type");
		if(formPage != null  && !formPage.equals("")){
			return formPage;
		}
		return "edit";
	}
	
	
	
	
	public String update(){
		FacesContext context = FacesContext.getCurrentInstance();
		String contents = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1").toString();
		bean.setContents(contents);
		if(bean.getTitle() == null || bean.getTitle().equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_1")));
			return "";
		}
		if(bean.getContents() == null ||  bean.getContents().equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_2")));
			return "";
		}
		getCourseForumService().update( courseId,bean.getTitle(), bean.getContents(), id);
		return "main";
	}
	

	@Override
	public void getpagedetail() {
		log.info("------------lookTitle = "+lookTitle);
		setItemCount(getCourseForumService().getManagerCount(courseId,lookTitle));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		list = getCourseForumService().pageQueryManager(start, pagesize,courseId,lookTitle );
	}
	
	public void deleteContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		log.info("----------------------- id = "+id);
		getCourseForumService().deleteContent(id);
		getpagedetail();
	}

	public void disableContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		log.info("----------------------- id = "+id);
		getCourseForumService().disableContent(id);
		getpagedetail();
	}
	
	public void enableContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		log.info("----------------------- id = "+id);
		getCourseForumService().enableContent(id);
		getpagedetail();
	}
	
	
	

	public List<CourseForumBean> getList() {
		return list;
	}

	public void changeCourseName(){
		log.info("----------------courseId = "+courseId);
		getpagedetail();
	}
	
	

	public void setList(List<CourseForumBean> list) {
		this.list = list;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getContents() {
		return contents;
	}


	public void setContents(String contents) {
		this.contents = contents;
	}

	public CourseForumBean getBean() {
		if(bean == null)
			bean = new CourseForumBean();
		return bean;
	}

	public void setBean(CourseForumBean bean) {
		this.bean = bean;
	}

	public List<CourseForumBean> getAnswerlist() {
		return answerlist;
	}

	public void setAnswerlist(List<CourseForumBean> answerlist) {
		this.answerlist = answerlist;
	}



	public String getId() {
		return id;
	}



	public void setId(String id) {
		this.id = id;
	}



	public String getType() {
		return type;
	}



	public void setType(String type) {
		this.type = type;
	}



	public String getCourseId() {
		//HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		return courseId;//session.getAttribute(Contants.COURSER_ID).toString();
	}



	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getSelectedCourseId() {
		return selectedCourseId;
	}

	public void setSelectedCourseId(String selectedCourseId) {
		this.selectedCourseId = selectedCourseId;
	}

	public SelectItem[] getCourseNameSelectItem() {
		List<CourseBean> course = getCourseForumService().getAllCourse();
		if(course != null){
			int len = course.size();
			courseNameSelectItem = new SelectItem[len];
			for(int i=0; i<len ; i++ ){
				courseNameSelectItem[i] = new  SelectItem(course.get(i).getId(),course.get(i).getCourse_name());
			}
			
		}else
			courseNameSelectItem = new SelectItem[0];
		return courseNameSelectItem;
	}

	public void setCourseNameSelectItem(SelectItem[] courseNameSelectItem) {
		this.courseNameSelectItem = courseNameSelectItem;
	}

	public String getLookTitle() {
		return lookTitle;
	}

	public void setLookTitle(String lookTitle) {
		this.lookTitle = lookTitle;
	}
	
}
